package com.yswy.common.wxOrder.V3;

import com.yswy.common.utils.StringUtils;
import lombok.Data;

import java.security.MessageDigest;
import java.util.*;

@Data
public class GenerateSignVo {
    private String appId ; // 替换为你的微信应用ID
    private String partnerId ; // 替换为你的商户ID
    private String prepayId ; // 替换为你的预支付交易会话ID
    private String nonceStr = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32); // 替换为随机生成的字符串
    private String timeStamp = new Date().getTime()+""; // 替换为时间戳
    private String packageValue = "Sign=WXPay" ; // 替换为扩展字段，通常为 "Sign=WXPay"
    private String sing ;//v3密钥
    private String trademsg ;//支付字符串
    private String signType; //签名方式



    public static String getSignStr(Map<String, Object> params) {
        List<String> paramStr = new ArrayList<>(params.size());
        for (String key : params.keySet()) {
            paramStr.add(key + "=" + params.get(key));
        }
        Collections.sort(paramStr);
        return StringUtils.join(paramStr, "&");
    }


    public String sign(String key){
        String sign = ("appId="+appId+"&");
        sign += ("nonceStr="+nonceStr+"&");
        sign += ("package="+prepayId+"&");
        sign += ("signType="+signType+"&");
        sign += ("timeStamp="+timeStamp+"&");
        sign += ("key="+key);
//        System.out.println("sign="+sign);
        return (MD5(sign).toUpperCase());
    }


    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) {
        try {
            java.security.MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString().toUpperCase();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
